/**
 * 
 */
package com.wisdytech.linkdcs.energy.action;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.session.UserSession;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.common.utils.ExcelUtil;
import com.wisdytech.linkdcs.energy.model.EquipmentEnergy;
import com.wisdytech.linkdcs.energy.model.EquipmentEnergyRecord;
import com.wisdytech.linkdcs.energy.service.IEquipmentEnergyAnalysisService;
import com.wisdytech.linkdcs.energy.service.IEquipmentEnergyRecordService;
import com.wisdytech.linkdcs.energy.service.IEquipmentEnergyService;
import com.wisdytech.linkdcs.energy.vo.EquipmentEnergyRecordVO;
import com.wisdytech.linkdcs.energy.vo.EquipmentEnergyVO;

/**
 * <p>
 * ClassName: 
 * </p>
 * <p>
 * Description: 
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-10-12
 * </p>
 */
@Controller
@RequestMapping("/equipment/energy")
public class EquipmentEnergyAction {
    /**
     * 注入设备能耗service
     */
    @Autowired
    @Qualifier("equipmentEnergyService")
    private IEquipmentEnergyService equipmentEnergyService;
    /**
     * 注入设备能耗查询service
     */
    @Autowired
    @Qualifier("equipmentEnergyRecordService")
    private IEquipmentEnergyRecordService equipmentEnergyRecordService;
    /**
     * 注入设备能耗分析service
     */
    @Autowired
    @Qualifier("equipmentEnergyAnalysisService")
    private IEquipmentEnergyAnalysisService equipmentEnergyAnalysisService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    /**
     * 
     * <p>Description: 能耗设备</p>
     * @param model
     * @return
     */
    @RequestMapping("/equipment/index")
    public String equipmentIndex(Model model,HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "equipEnergy/index";
    }
    
    /**
     * 
     * <p>Description: 能耗查询</p>
     * @param model
     * @return
     */
    @RequestMapping("/search/index")
    public String searchIndex(Model model,HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "equipEnergy/detail";
    }
    
    /**
     * 
     * <p>Description: 能耗分析</p>
     * @param model
     * @return
     */
    @RequestMapping("/analysis/index")
    public String analysisIndex(Model model,HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "equipEnergy/analysis";
    }
    
    /**
     * 分页条件查询设备能耗管理列表
     * @param code 能耗设备编号
     * @param type 能耗设备类型
     * @param filter 分页参数
     * @return 设备能耗管理列表
     */
    @RequestMapping(value = "/listEquipmentEnergyByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<EquipmentEnergyVO> listEquipmentEnergyByFilter(String code, String type, Filter filter) {
        PageInfo<EquipmentEnergy> equipEnergys = equipmentEnergyService.listEquipmentEnergyByFilter(code, type, filter);
        //转换为前台结果集
        List<EquipmentEnergy> energyList = equipEnergys.getList();
        List<EquipmentEnergyVO> energyVOList=new ArrayList<EquipmentEnergyVO>();
        for(EquipmentEnergy energy: energyList){
            energyVOList.add(DataTransformUtil.changeEnergyVO(energy));
        }
        return new StandardTableResult<EquipmentEnergyVO>(equipEnergys.getTotal(),energyVOList);
    }
    
    /**
     * <p>Description: 保存能耗设备对象</p> 
     * @param model 设备对象
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveEquipmentEnergy/save.mvc")
    @ResponseBody
    public StandardResult saveEquipmentEnergy(EquipmentEnergy energy, HttpSession session) {
    	
    	if("1".equals(energy.getType())) {
    		energy.setType("水表");
    	}else {
    		energy.setType("电表");
    	}
    	
        SysUser currentUser = BaseUtil.getCurrentUser();
        String loginName = currentUser.getUsername();
        if (StringUtils.isBlank(energy.getPkId())) {
            return equipmentEnergyService.addEquipmentEnergy(energy, loginName);
        }else {
            return equipmentEnergyService.updateEquipmentEnergy(energy, loginName);
        }
    }
    /**
     * 通过主键id删除对应设备能源信息
     * @param pkId 主键id
     * @return 删除结果
     */
    @RequestMapping(value = "/delEquipmentEnergyById/del.mvc")
    @ResponseBody
    public StandardResult delEquipmentEnergyById (String pkId) {
        return equipmentEnergyService.delEquipmentEnergyById(pkId);
    }
    /**
     * 通过主键id查询对应的能源对象信息
     * @param pkId 主键id
     * @return 能源对象
     */
    @RequestMapping(value = "/getEquipmentRegisterById/get.mvc")
    @ResponseBody
    public EquipmentEnergyVO getEquipmentEnergyById(String pkId) {
        EquipmentEnergy energy = equipmentEnergyService.getEquipmentEnergyById(pkId);
        EquipmentEnergyVO energyVO = new EquipmentEnergyVO();
        energyVO = DataTransformUtil.changeEnergyVO(energy);
        return energyVO;
    }
    
    /**
     * 分页条件查询设备能耗查询页面信息
     * @param code 能耗设备编号
     * @param type 能耗设备类型
     * @param filter 分页参数
     * @return 设备能耗查询页面信息
     */
    @RequestMapping(value = "/listEquipmentEnergyRecordByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<EquipmentEnergyRecordVO> listEquipmentEnergyRecordByFilter(String code, String startTime, String endTime, String operationId, Filter filter) {
        PageInfo<EquipmentEnergyRecord> equipEnergyRecords = equipmentEnergyRecordService.listEquipmentEnergyRecordByFilter(code, startTime, endTime,operationId, filter);
        //转换为前台结果集
        List<EquipmentEnergyRecord> energyRecordList = equipEnergyRecords.getList();
        List<EquipmentEnergyRecordVO> energyRecordVOList=new ArrayList<EquipmentEnergyRecordVO>();
        for(EquipmentEnergyRecord energy: energyRecordList){
            energyRecordVOList.add(DataTransformUtil.changeEnergyRecordVO(energy));
        }
        return new StandardTableResult<EquipmentEnergyRecordVO>(equipEnergyRecords.getTotal(),energyRecordVOList);
    }
    /**
     * 条件查询返回用电量和用水量的统计图
     * @param code 能耗设备编号
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param operationId 所属工序
     * @return 用电量和用水量的统计图
     */
    @RequestMapping(value = "/getColumn/get.mvc")
    @ResponseBody
    public List<EquipmentEnergyRecord> getColumn(String code, String startTime, String endTime, String operationId) {
        List<EquipmentEnergyRecord> recordLists = equipmentEnergyAnalysisService.getColumn(code, startTime, endTime, operationId);
        return recordLists;
    }
    
    /**
     * 条件查询返回用电量的统计图
     * @param code 能耗设备编号
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param operationId 所属工序
     * @return 用电量统计图
     */
    @RequestMapping(value = "/getElectricPieRate/get.mvc")
    @ResponseBody
    public List<EquipmentEnergyRecord> getElectricPieRate(String code, String startTime, String endTime, String operationId) {
        List<EquipmentEnergyRecord> recordLists = equipmentEnergyAnalysisService.getElectricPieRate(code, startTime, endTime, operationId);
        return recordLists;
    }
    /**
     * 条件查询返回用水量的统计图
     * @param code 能耗设备编号
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param operationId 所属工序
     * @return 用水量统计图
     */
    @RequestMapping(value = "/getWaterPieRate/get.mvc")
    @ResponseBody
    public List<EquipmentEnergyRecord> getWaterPieRate(String code, String startTime, String endTime, String operationId) {
        List<EquipmentEnergyRecord> recordLists = equipmentEnergyAnalysisService.getWaterPieRate(code, startTime, endTime, operationId);
        return recordLists;
    }
    
    /**
     * 条件查询返回用电量的折柱混合图
     * @param code 能耗设备编号
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param operationId 所属工序
     * @return 用电量折柱混合图
     */
    @RequestMapping(value = "/getElectricLineColumn/get.mvc")
    @ResponseBody
    public List<EquipmentEnergyRecord> getElectricLineColumn(String code, String startTime, String endTime, String operationId) {
        List<EquipmentEnergyRecord> recordLists = equipmentEnergyAnalysisService.getElectricLineColumn(code, startTime, endTime, operationId);
        return recordLists;
    }
    
    /**
     * 条件查询返回用电量的折柱混合图
     * @param code 能耗设备编号
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param operationId 所属工序
     * @return 用电量折柱混合图
     */
    @RequestMapping(value = "/getWaterLineColumn/get.mvc")
    @ResponseBody
    public List<EquipmentEnergyRecord> getWaterLineColumn(String code, String startTime, String endTime, String operationId) {
        List<EquipmentEnergyRecord> recordLists = equipmentEnergyAnalysisService.getWaterLineColumn(code, startTime, endTime, operationId);
        return recordLists;
    }
    /**
     * 通过查询条件查询能耗设备信息
     * @param search 查询条件
     * @return 能耗设备信息
     */
    @RequestMapping(value = "/listAllEquipmentEnergy/get.mvc")
    @ResponseBody
    public List<EquipmentEnergy> listAllEquipmentEnergy(@ModelAttribute("search") String search) {
        return equipmentEnergyService.listAllEquipmentEnergy(search);
    }
   /**
    * 导出设备能耗查询页面的数据信息
    * @param request 
    * @param response
    * @param filter
    * @throws Exception
    */
    @RequestMapping(value = "/exportDetail/export.mvc")
    @ResponseBody
    public void exportDetail(HttpServletRequest request, HttpServletResponse response, Filter filter) throws Exception {
        SimpleDateFormat dateFormat;
        dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String[] columnNames = null;
        String[] fileNames = null;
        String fileName = "能耗信息";
        String code = request.getParameter("code");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String operationId = request.getParameter("operationId");
        if ("null".equals(code)) {
            code = "";
        }
        if ("null".equals(operationId)) {
            operationId = "";
        }
        filter.setSize(65535);
        PageInfo<EquipmentEnergyRecord> energyRecords;
        energyRecords = equipmentEnergyRecordService.listEquipmentEnergyRecordByFilter(code, startTime, endTime,operationId, filter);
        List<EquipmentEnergyRecord> energyRecordList= energyRecords.getList();
        String finalFileName = null;  
        final String userAgent = request.getHeader("USER-AGENT"); 
        if(StringUtils.contains(userAgent, "MSIE")){
            //IE浏览器  
            finalFileName = URLEncoder.encode(fileName,"UTF8");  
        }else {
            finalFileName = fileName;
        }
        columnNames = new String[] { "能耗设备编号", "设备类型", "所属工序", "所属车间", "能耗名称", "能耗单位", "能耗数据","采集时间"};
        fileNames = new String[] { "code", "type", "operationName","workshop","energyName", "energyUnit", "value","createDate"};
        ExcelUtil.exportExcel(response, energyRecordList, EquipmentEnergyRecord.class, columnNames, fileNames,
                finalFileName + dateFormat.format(new Date()));
    }
    
}
